package com.md.api.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeVehicleLicenseRequest;
import com.aliyun.ocr_api20210707.models.RecognizeVehicleLicenseResponse;
import com.aliyun.ocr_api20210707.models.RecognizeVehicleLicenseResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.conn.BasicManagedEntity;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AliOcrUtil
 * @Description 阿里云ocr相关工具类
 * @Date 2022/5/26 20:00
 * @Version 1.0
 */
@Component
@Slf4j
public class AliOcrUtil {

    public static final String accessKeyId = "LTAI5tRNAHS4btPFXiCsf5mH";
    public static final String accessKeySecret = "dmNepxJuFsoRQk5Z6JTqrU9tXPbPZr";


    public static com.aliyun.ocr_api20210707.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        Config config = new Config()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId(accessKeyId)
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/ocr-api
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new Client(config);
    }

    public static JSONObject identify(String url)  {
        try {
            Client client = AliOcrUtil.createClient();
            RecognizeVehicleLicenseRequest request = new RecognizeVehicleLicenseRequest()
                    .setUrl(url);
            RuntimeOptions runtime = new RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            RecognizeVehicleLicenseResponse response = client.recognizeVehicleLicenseWithOptions(request, runtime);
            if (response.getStatusCode() == 200){
                RecognizeVehicleLicenseResponseBody body = response.getBody();
                JSONObject resultData = JSONObject.parseObject(body.getData());
                log.info("OCR返回：" + resultData);
                JSONObject result =  new JSONObject();
                JSONObject data = resultData.getJSONObject("data");
                if (null!=data&&null!=data.get("face")){
                    JSONObject faceData = data.getJSONObject("face").getJSONObject("data");
                    result.put("face",faceData);
                }
                if (null!=data&&null!=data.get("back")){
                    JSONObject backData = data.getJSONObject("back").getJSONObject("data");
                    result.put("back",backData);
                }
                return result;
            }
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            log.error(error.getMessage());
        }
        return null;
    }


    public static void main(String[] args) {
        JSONObject identify = AliOcrUtil.identify("https://cgj023.oss-cn-beijing.aliyuncs.com/face.jpg?Expires=1743995519&OSSAccessKeyId=TMP.3Kpm9kktt9oajebgyP5knzF5CtKRP8PjEBcdZL2Cytuehae6oLw53FK3vFSM2gnbfdeNHYSfwLSJL4Ea5PhovY6BMZifZY&Signature=7B%2FGbxu4nzbTAGWYO1NY84sbSw8%3D");
        log.info("identify:"+identify);
    }
}
